import axios from '../request/http'

export function randomText (length) {
  const data =
    ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
      "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
      "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
      "s", "t", "u", "v", "w", "x", "y", "z"];
  let nums = "";
  for (let i = 0; i < length; i++) {
    const r = parseInt(Math.random() * 61, 10);
    nums += data[r];
  }
  return nums;
}

function testP (value) {
  return new Promise((resolve, reject) => {
    if (value > 5){
      alert(3)
      resolve(9)
    } else {
      reject(1)
      alert(0)
    }
  })
}

/*
* 判断是否为{} null
*/
//es5的方法
export function isEmptyObject(obj) {
  for (var key in obj){
    return !obj.hasOwnProperty(key)
  }
  return true
};
//es6的方法
export function isEmptyObjectES6(obj) {
  if(Object.getOwnPropertyNames(obj).length){
    return false
  }
  return true
}

export function dataURLtoBlob(dataurl){
  var arr = dataurl.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
}

export function blobToFile(theBlob, fileName){
  theBlob.lastModifiedDate = new Date();
  if (fileName == null) {
    fileName = randomText(6)
  }
  theBlob.name = fileName;
  return theBlob;
}

export function dataURLToFile(dataurl){
  var blob = dataURLtoBlob(dataurl)
  return blobToFile(blob)
}

export function isEmptyStr(obj) {
  if (typeof obj === 'undefined' || obj == null || obj == '') {
    return true;
  } else {
    return false;
  }
}
export function isEmpty(v){
  switch (typeof v){
    case 'undefined' : return true;
    case 'string' : if(v.trim().length == 0) return true;
    case 'boolean' : if(!v) return true; break;
    case 'number' : if(0 === v) return true; break;
    case 'object' :
      if(null === v) return true;
      if(undefined !== v.length && v.length==0) return true;
      for(var k in v){return false;} return true;
      break;
  }
  return false;
}

/**
 * Get the first item that pass the test
 * by second argument function
 *
 * @param {Array} list
 * @param {Function} f
 * @return {*}
 */
function find (list, f) {
  return list.filter(f)[0]
}

export function deepCopy (obj, cache) {
  if ( cache === void 0 ) cache = [];

  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }

  // if obj is hit, it is in circular structure
  var hit = find(cache, function (c) { return c.original === obj; });
  if (hit) {
    return hit.copy
  }

  var copy = Array.isArray(obj) ? [] : {};
  // put the copy into cache at first
  // because we want to refer it in recursive deepCopy
  cache.push({
    original: obj,
    copy: copy
  });

  Object.keys(obj).forEach(function (key) {
    copy[key] = deepCopy(obj[key], cache);
  });

  return copy
}

// 定义一个深拷贝函数  接收目标target参数
function deepClone(target) {
  // 定义一个变量
  let result;
  // 如果当前需要深拷贝的是一个对象的话
  if (typeof target === 'object') {
    // 如果是一个数组的话
    if (Array.isArray(target)) {
      result = []; // 将result赋值为一个数组，并且执行遍历
      for (let i in target) {
        // 递归克隆数组中的每一项
        result.push(deepClone(target[i]))
      }
      // 判断如果当前的值是null的话；直接赋值为null
    } else if(target===null) {
      result = null;
      // 判断如果当前的值是一个RegExp对象的话，直接赋值
    } else if(target.constructor===RegExp){
      result = target;
    }else {
      // 否则是普通对象，直接for in循环，递归赋值对象的所有值
      result = {};
      for (let i in target) {
        result[i] = deepClone(target[i]);
      }
    }
    // 如果不是对象的话，就是基本数据类型，那么直接赋值
  } else {
    result = target;
  }
  // 返回最终结果
  return result;
}

//扩展数组方法:删除指定元素
Array.prototype.rmove = function(val) {
  var index = this.indexOf(val);
  while(index>-1){
    this.splice(index, 1);
    index = this.indexOf(val);
  }
  return this;
};
//扩展数组方法:删除指定元素
Array.prototype.removeAll = function() {
  this.splice(0,this.length);
};


export default {
// Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function (Vue) {
    Vue.prototype.randomText = (length) => randomText(length)
    Vue.prototype.testP = (param) => testP(param)
    Vue.prototype.dataURLtoBlob = (param) => dataURLtoBlob(param)
    Vue.prototype.blobToFile = (blob, fileName) => blobToFile(blob, fileName)
    Vue.prototype.dataURLToFile = (param) => dataURLToFile(param)
    Vue.prototype.isEmptyStr = (param) => isEmptyStr(param)
    Vue.prototype.isEmpty = (param) => isEmpty(param)
    Vue.prototype.deepCopy = (obj, cache) => deepCopy(obj, cache)
  }
}



